package com.dgj.log.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.util.NutMap;

import com.dgj.log.dao.ILogDao;
import com.dgj.log.entities.Log;
import com.dgj.mongo.EntityMapping;
import com.dgj.mongo.Pager;
import com.dgj.mongo.annotation.Collection;
import com.dgj.mongo.dao.MongoDao;
import com.dgj.mongo.utils.Mongos;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 * @author 崔海军 create at 2015年7月9日 下午4:16:48
 */
@IocBean(name = "logDaos")
public class LogDaoImpl extends MongoDao<Log> implements ILogDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.mongo.dao.MongoDao#init()
	 */
	@Override
	protected void init() {
		mapping = new EntityMapping<Log>() {

			@Override
			public DBObject toDBObject(Log log) {
				return Mongos.obj2dbo(log);
			}

			@Override
			public DBCollection getCollection() {
				return db.getCollection(Log.class.getAnnotation(Collection.class).value());
			}

			@Override
			public Log get(DBObject dbo) {
				NutMap map = Mongos.dbo2map(dbo);
				map.put("id", dbo.get("_id"));

				NutMap loggerName = Lang.obj2nutmap(dbo.get("loggerName"));
				loggerName.put("packageName", loggerName.get("package"));

				map.put("loggerName", loggerName);
				Log log = Lang.map2Object(map, Log.class);
				return log;
			}
		};
		collection = mapping.getCollection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.log.dao.ILogDao#count(java.lang.String)
	 */
	@Override
	public long count(String appName) {
		return db.getCollection(appName).count();
	}

	@Override
	public List<Log> listAllByPage(Pager pager, DBObject orderBy, String appName) {
		List<Log> ts = new ArrayList<Log>();
		DBCursor cursor = db.getCollection(appName).find().sort(orderBy).skip(pager.start()).limit(pager.getPageSize());
		while (cursor.hasNext()) {
			ts.add(mapping.get(cursor.next()));
		}
		return ts;
	}

	@Override
	public Log findOneByCnd(Object obj, String appName) {
		DBObject query = null;
		if (obj instanceof DBObject) {
			query = (DBObject) obj;
		} else {
			query = Mongos.obj2dbo(obj);
		}
		DBObject object = db.getCollection(appName).findOne(query);
		if (object == null) {
			return null;
		}
		return mapping.get(object);
	}

	@Override
	public List<Log> findByPage(Object obj, Pager pager, DBObject orderBy, String appName) {
		DBObject query = null;
		if (obj instanceof DBObject) {
			query = (DBObject) obj;
		} else {
			query = Mongos.obj2dbo(obj);
		}
		List<Log> ts = new ArrayList<Log>();
		DBCursor cursor = db.getCollection(appName).find(query).sort(orderBy).skip(pager.start()).limit(pager.getPageSize());
		while (cursor.hasNext()) {
			ts.add(mapping.get(cursor.next()));
		}
		return ts;
	}

	@Override
	public long countByCnd(Object obj, String appName) {
		DBObject query = null;
		if (obj instanceof DBObject) {
			query = (DBObject) obj;
		} else {
			query = Mongos.obj2dbo(obj);
		}
		return db.getCollection(appName).count(query);
	}

}
